#import "UIView+XMPlaceholder.h"
#import <objc/runtime.h>
//static CGFloat const kTopHeight = 100.f;

@implementation UIView (XMPlaceholder)
- (void)showNetWorkErrorView:(void (^_Nullable)(void))retryBlock TopSpacing:(CGFloat)topSpacing {
    [self showStatusView:YHViewStatusTypeNetWorkError spacing:topSpacing retryBlock:retryBlock];
}
- (void)hideNetWorkErrorView {
    [self hideStatusView];
}
- (void)showStatusView:(YHViewStatusType)type {
    [self showStatusView:type spacing:kNavBarAndStatusBarHeight + 70 retryBlock:nil];
}
- (void)showStatusView:(YHViewStatusType)type
               spacing:(CGFloat)spacing {
    [self showStatusView:type spacing:spacing retryBlock:nil];
}
- (void)showStatusView:(YHViewStatusType)type
               spacing:(CGFloat)spacing
            retryBlock:(void (^_Nullable)(void))retryBlock {
    self.retryBlock = retryBlock;
    self.statusType = type;
    [self addSubview:self.statusView];
    [self.statusView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, self.bounds.size.height));
        make.center.equalTo(self);
    }];
    while (self.statusView.subviews.count) {
        [self.statusView.subviews.lastObject removeFromSuperview];
    }
    [self.statusView addSubview:self.statusImageView];
    [self.statusView addSubview:self.statusTitleLabel];
    [self.statusImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_centerY);
        make.centerX.equalTo(self);
        if (type == YHViewStatusTypeEmptyData) {
            make.width.mas_equalTo(200.f);
            make.height.mas_equalTo(200.f);
        } else {
            make.width.mas_equalTo(200.f);
            make.height.mas_equalTo(200.f);
        }
    }];
    [self.statusTitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.statusImageView.mas_bottom).offset(0);
        make.height.mas_equalTo(20);
    }];
    switch (type) {
        case YHViewStatusTypeEmptyData: {
            self.statusImageView.image = kGetImage(@"empty_normal");
            self.statusTitleLabel.text = @"暂无数据";
        } break;
            
        case YHViewStatusTypeEmptyNotice: {
            self.statusImageView.image = kGetImage(@"empty_normal");
            self.statusTitleLabel.text = @"暂时还没有公告哦～";
        } break;
            
        case YHViewStatusTypeEmptyLike: {
            self.statusImageView.image = kGetImage(@"empty_normal");
            self.statusTitleLabel.text = @"您还没有为Ta人点赞哦～";
            [self.retryButton setTitle:@"去点赞" forState:UIControlStateNormal];
        } break;
            
        case YHViewStatusTypeNetWorkError: {
            self.statusImageView.image = [self getImageFromBundleWithName:@"ic_neterror"];
            self.statusTitleLabel.text = @"网络异常";
            [self.retryButton setTitle:@"刷新网络" forState:UIControlStateNormal];
        } break;
        default:
            break;
    }
    if (type == YHViewStatusTypeNetWorkError 
//        ||
//        type == YHViewStatusTypeEmptyLike
        ) {
        [self.statusView addSubview:self.retryButton];
        [self.retryButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self.statusTitleLabel.mas_bottom).offset(10.f);
            make.width.mas_equalTo(50.f);
            make.height.mas_equalTo(20.f);
        }];
        
        [self.statusView addSubview:self.lineView];
        [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(42, 1));
            make.centerX.equalTo(self);
            make.top.equalTo(self.retryButton.mas_bottom).offset(0.f);
        }];
        
    }
}
- (void)hideStatusView {
    [self.statusView removeFromSuperview];
}
#pragma mark - Method
- (void)retryButtonClick {
    if (self.retryBlock) {
        self.retryBlock();
    }
}
#pragma mark - Setter&Getter
- (void)setStatusView:(UIView *)statusView {
    objc_setAssociatedObject(self, @selector(statusView), statusView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)statusView {
    UIView *_statusView = objc_getAssociatedObject(self, @selector(statusView));
    if (!_statusView) {
        _statusView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, UIScreen.mainScreen.bounds.size.height)];
        _statusView.backgroundColor = [UIColor whiteColor];
        objc_setAssociatedObject(self, @selector(statusView), _statusView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return _statusView;
}

- (void)setLineView:(UIView *)lineView {
    objc_setAssociatedObject(self, @selector(lineView), lineView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)lineView {
    UIView *_lineView = objc_getAssociatedObject(self, @selector(lineView));
    if (!_lineView) {
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 42, 1)];
        _lineView.backgroundColor = kAppMainColor;
        objc_setAssociatedObject(self, @selector(lineView), _lineView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return _lineView;
}

- (void)setStatusImageView:(UIImageView *)statusImageView {
    objc_setAssociatedObject(self, @selector(statusImageView), statusImageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIImageView *)statusImageView {
    UIImageView *_statusImageView = objc_getAssociatedObject(self, @selector(statusImageView));
    if (!_statusImageView) {
        _statusImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        objc_setAssociatedObject(self, @selector(statusImageView), _statusImageView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return _statusImageView;
}
- (void)setStatusTitleLabel:(UIImageView *)statusTitleLabel {
    objc_setAssociatedObject(self, @selector(statusTitleLabel), statusTitleLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UILabel *)statusTitleLabel {
    UILabel *_statusTitleLabel = objc_getAssociatedObject(self, @selector(statusTitleLabel));
    if (!_statusTitleLabel) {
        _statusTitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _statusTitleLabel.textColor = kHEX(@"9A9A9A");
        _statusTitleLabel.font = [UIFont systemFontOfSize:14];
        _statusTitleLabel.backgroundColor = [UIColor clearColor];
        objc_setAssociatedObject(self, @selector(statusTitleLabel), _statusTitleLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return _statusTitleLabel;
}
- (void)setRetryButton:(UIButton *)retryButton {
    objc_setAssociatedObject(self, @selector(retryButton), retryButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIButton *)retryButton {
    UIButton *_retryButton = objc_getAssociatedObject(self, @selector(retryButton));
    if (!_retryButton) {
        _retryButton = [[UIButton alloc] initWithFrame:CGRectZero];
        _retryButton.backgroundColor = [UIColor whiteColor];
        [_retryButton setTitleColor:kAppMainColor forState:UIControlStateNormal];
        _retryButton.titleLabel.font = [UIFont systemFontOfSize:14];
//        _retryButton.layer.cornerRadius = 24.f;
//        _retryButton.layer.masksToBounds = YES;
//        _retryButton.layer.borderWidth = 1.f;
//        _retryButton.layer.borderColor = UIColor.blackColor.CGColor;
        [_retryButton addTarget:self action:@selector(retryButtonClick) forControlEvents:UIControlEventTouchUpInside];
        objc_setAssociatedObject(self, @selector(retryButton), _retryButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return _retryButton;
}
- (void)setRetryBlock:(void (^)(void))retryBlock {
    objc_setAssociatedObject(self, @selector(retryBlock), retryBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (YHRetryBlock)retryBlock {
    return objc_getAssociatedObject(self, @selector(retryBlock));
}
- (void)setStatusType:(YHViewStatusType)statusType {
    objc_setAssociatedObject(self, @selector(statusType), @(statusType), OBJC_ASSOCIATION_ASSIGN);
}
- (YHViewStatusType)statusType {
    return [objc_getAssociatedObject(self, _cmd) integerValue];
}

- (UIImage *)getImageFromBundleWithName:(NSString *)imageName {
    NSURL *bundleURL = [[NSBundle mainBundle] URLForResource:@"YHPlaceholderView" withExtension:@"bundle"];
    if (bundleURL) {
        NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
        NSInteger scale = [[UIScreen mainScreen] scale];
        NSString *imgName = [NSString stringWithFormat:@"%@@%zdx.png", imageName, scale];
        return [UIImage imageWithContentsOfFile:[bundle pathForResource:imgName ofType:nil]];
    }
    return nil;
}

@end
